package com.yq.tanhua.ui.chat.chatlist

import android.app.Activity
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import com.hyphenate.chat.EMClient
import com.hyphenate.chat.EMMessage
import com.hyphenate.chat.EMTextMessageBody
import com.yq.tanhua.utils.UserCenter
import org.json.JSONObject

private val ChatBubbleShapeLeft = RoundedCornerShape(4.dp, 20.dp, 20.dp, 20.dp)
private val ChatBubbleShapeRight = RoundedCornerShape(20.dp, 4.dp, 20.dp, 20.dp)

@Composable
fun TextMessage(message: EMMessage) {
    val isSender = message.from == EMClient.getInstance().currentUser
    val textBody = message.body as EMTextMessageBody
    if (isSender) {
        Row(verticalAlignment = Alignment.Top, modifier = Modifier.padding(20.dp)) {
            Spacer(modifier = Modifier.weight(1f))
            Column {
                Text(
                    text = message.from,
                    modifier = Modifier.align(Alignment.End)
                )
                Text(
                    text = textBody.message,
                    modifier = Modifier
                        .padding(top = 6.dp)
                        .clip(ChatBubbleShapeRight)
                        .background(Color.White)
                        .padding(10.dp)
                        .wrapContentSize()
                        .widthIn(max = 200.dp)
                )
            }
            Spacer(modifier = Modifier.width(10.dp))
            AsyncImage(
                model = "",
                contentDescription = "",
                modifier = Modifier
                    .size(38.dp)
                    .clip(
                        CircleShape
                    )
                    .background(Color.Gray)
            )
        }
    } else {
        Row(verticalAlignment = Alignment.Top, modifier = Modifier.padding(20.dp)) {
            AsyncImage(
                model = "",
                contentDescription = "",
                modifier = Modifier
                    .size(38.dp)
                    .clip(
                        CircleShape
                    )
                    .background(Color.Gray)
            )
            Spacer(modifier = Modifier.width(10.dp))
            Column {
                Text(text = message.from)
                Text(
                    text = textBody.message, modifier = Modifier
                        .padding(top = 6.dp)
                        .clip(ChatBubbleShapeLeft)
                        .background(Color.White)
                        .padding(10.dp)
                        .wrapContentSize()
                        .widthIn(max = 200.dp)

                )
            }


        }
    }


}

@Composable
fun CustomMessage(message: EMMessage,agressCallback:(()->Unit)?=null) {
    val context  = LocalContext.current

    val contentBody = message.body as EMTextMessageBody
    val content = JSONObject(contentBody.message)
    val nickName = content.getString("nickname")
    val question = "我的陌生人问题： " + content.getString("strangerQuestion")
    val answer = nickName + "回答了你的问题： " + content.getString("reply")
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .padding(20.dp)
            .clip(RoundedCornerShape(4.dp))
            .background(Color.White)
            .padding(20.dp),
    ) {
        Text(text = nickName, style = TextStyle(color = Color.Black, fontWeight = FontWeight.Bold))
        Text(text = question, modifier = Modifier.padding(top = 15.dp, bottom = 10.dp))
        Text(text = answer)
        Spacer(modifier = Modifier.height(10.dp))
        Row {
            Text(
                text = "聊一下",
                modifier = Modifier.weight(1f)
                    .clickable {
                        agressCallback?.invoke()
                    },
                style = TextStyle(
                    color = Color.Blue,
                    fontWeight = FontWeight.Bold,
                    textAlign = TextAlign.Center
                )
            )
            Text(
                text = "算了吧",
                modifier = Modifier.weight(1f).clickable {
                    ( context as Activity).finish()
                },
                style = TextStyle(
                    color = Color.LightGray,
                    fontWeight = FontWeight.Bold,
                    textAlign = TextAlign.Center
                )
            )
        }
    }
}